package tests;

import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;

import java.util.List;

import mailsystem.Mailbox;
import mailsystem.Message;
import mailsystem.persistance.MailsystemPersistance;
import mailsystem.persistance.database.MailBoxDB;
import mailsystem.persistance.database.KeptMessageDB;

import org.junit.Before;
import org.junit.Test;

public class KeptMessageDBTest {
	KeptMessageDB messages;
	Mailbox box1;
	MailsystemPersistance persistanceMock = mock(MailsystemPersistance.class);
	
	@Before
	public void doBefore()
	{
		MailBoxDB boxes;
		
		messages = new KeptMessageDB(persistanceMock);
		messages.changeToTestMode();
		boxes = new MailBoxDB(persistanceMock);
		boxes.changeToTestMode();
		box1 = boxes.listAll().get(0);
	}

	@Test
	public void testAdd() {
		int before = messages.listAll(box1).size();
		Message msg = new Message("Holaa Test", 0, false);
		messages.add(box1, msg);
		int after = messages.listAll(box1).size();
		
		assertEquals(before + 1, after);
		assertFalse(msg.getMessageId() == 0);
	}

	@Test
	public void testDelete() {
		List<Message> msgs = messages.listAll(box1);
		int before = msgs.size();
		
		Message msg = msgs.get(0);
		messages.delete(msg);
		int after = messages.listAll(box1).size();
		
		assertEquals(before - 1, after);
	}

	@Test
	public void testListAll() {
		int before = messages.listAll(box1).size();
		Message msg = new Message("Holaa Test", 0, false);
		messages.add(box1, msg);
		int after = messages.listAll(box1).size();
		
		assertEquals(before + 1, after);
	}
}
